<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Report extends CI_Controller {
	public function index(){
		#$this->session->userdata('user_id').'|'.$this->session->userdata('user_name');
		$this->layout->setLayout('default');
		$this->layout->setTitle('Administrator | Report');
		$month = isset($_GET['month']) ? date('Y').'-'.str_pad(quotes_to_entities($_GET['month']), 2, "0", STR_PAD_LEFT) : date('Y-m');
		$year = isset($_GET['year']) ? $_GET['year'] : date('Y');
		if ($year == "") $year = date('Y');
		$month = isset($_GET['month']) ? $year . '-'.str_pad(quotes_to_entities($_GET['month']), 2, "0", STR_PAD_LEFT) : date('Y-m');
		$arrDate = explode("-", $month);
		$data['month'] = $arrDate[1].'-'.$arrDate[0];
		//Tinh so ngay trong thang
		$day = cal_days_in_month(CAL_GREGORIAN, $arrDate[1],$arrDate[0]);
		$total = 0;
		$totalcustotmer = 0;
		$arrData = array();
		$arrData1 = array();
		$arrData2 = array();
		// Use Procedure in Phpmyadmin ======================================
		$con = mysql_connect($this->db->hostname, $this->db->username, $this->db->password, false,65536);
		mysql_select_db($this->db->database, $con);
		
		$sql = "call spSubTotalMonthAll('".$month."-01 00:00:00','".$month."-".$day." 23:59:59');";
		$queryUser1 = mysql_query($sql);
		
		if ($queryUser1 === FALSE) {
			die(mysql_error());
		}
		$smscode = '';
		$j = 0;
		while($row = mysql_fetch_array($queryUser1))
		{				
			$arrData[$row['earm_date']] += intval($row['earm_day']);
			$arrData1[$row['Website_Name']] += intval($row['earm_day']);
			$arrData2[$row['Website_Name']]  += intval($row['earm_day_site']);
			$total += intval($row['earm_day']);
			$totalcustotmer  += intval($row['earm_day_site']);			
			if($smscode != $row['Website_Name']){
				$j += 1;
				$smscode = $row['Website_Name'];
				$data['sitename'][$j]['website_name'] = $smscode;
			}
		}
		
		$data['loop'] = $j;
		mysql_close($con);
		// ==================================================================		
		
		
		$data['earn_this_month'] = $total;
		$data['earn_this_month_customer'] = $totalcustotmer;		
		$data['month_data'] = $arrData;
		$data['month_data_site1'] = $arrData1;
		$data['month_data_site2'] = $arrData2;		
		$data['currentUser'] = $this->session->userdata('user_name');
		$this->layout->view(THEME_DIR.'/report_index', $data);
	}
}